<template>
  <div class="order-page">
    <div class="order-container">
      <!-- 支付成功页面 -->
      <div v-if="paymentSuccess" class="success-page">
        <div class="success-content">
          <div class="success-icon">
            <el-icon class="check-icon"><CircleCheck /></el-icon>
          </div>
          <h1 class="success-title">支付成功！</h1>
          <p class="success-message">您的订单已确认，祝您旅途愉快</p>
          
          <div class="order-info-card">
            <div class="info-item">
              <span class="label">景点名称</span>
              <span class="value">{{ attraction.name }}</span>
            </div>
            <div class="info-item">
              <span class="label">游玩日期</span>
              <span class="value">{{ visitDate }}</span>
            </div>
            <div class="info-item">
              <span class="label">门票数量</span>
              <span class="value">{{ ticketCount }} 张</span>
            </div>
            <div class="info-item highlight">
              <span class="label">支付金额</span>
              <span class="value">¥{{ ticketTotal }}</span>
            </div>
          </div>

          <div class="countdown-tip">
            <el-icon><Clock /></el-icon>
            <span>{{ countdown }} 秒后自动关闭页面</span>
          </div>

          <el-button class="close-btn" size="large" @click="handleClose">
            立即关闭
          </el-button>
        </div>
      </div>

      <!-- 订单信息 -->
      <div v-else class="order-content">
        <!-- 头部标题 -->
        <div class="page-header">
          <h1 class="page-title">确认订单</h1>
          <p class="page-subtitle">请仔细核对订单信息</p>
        </div>

        <!-- 景点信息卡片 -->
        <div class="attraction-section">
          <div class="attraction-banner">
            <img :src="attraction.image" :alt="attraction.name" class="banner-image" />
            <div class="banner-overlay">
              <h2 class="attraction-title">{{ attraction.name }}</h2>
              <div class="attraction-location">
                <el-icon><Location /></el-icon>
                <span>{{ attraction.location }}</span>
              </div>
            </div>
          </div>
        </div>

        <!-- 订单表单 -->
        <div class="order-form">
          <!-- 门票信息 -->
          <div class="form-section">
            <h3 class="form-title">门票信息</h3>
            <div class="ticket-selector">
              <div class="ticket-info">
                <div class="ticket-name">成人票</div>
                <div class="ticket-price">¥{{ attraction.price }}/人</div>
              </div>
              <div class="ticket-counter">
                <span class="counter-label">数量</span>
                <el-input-number v-model="ticketCount" :min="1" :max="10" />
              </div>
            </div>
          </div>

          <!-- 游玩日期 -->
          <div class="form-section">
            <h3 class="form-title">游玩日期</h3>
            <el-date-picker
              v-model="visitDate"
              type="date"
              placeholder="选择游玩日期"
              :disabled-date="disabledDate"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              style="width: 100%"
              size="large"
            />
          </div>

          <!-- 联系人信息 -->
          <div class="form-section">
            <h3 class="form-title">联系人信息</h3>
            <div class="contact-form">
              <el-input
                v-model="contactForm.name"
                placeholder="请输入联系人姓名"
                size="large"
                clearable
              >
                <template #prefix>
                  <el-icon><User /></el-icon>
                </template>
              </el-input>
              <el-input
                v-model="contactForm.phone"
                placeholder="请输入手机号"
                size="large"
                clearable
              >
                <template #prefix>
                  <el-icon><Phone /></el-icon>
                </template>
              </el-input>
            </div>
          </div>

          <!-- 订单金额 -->
          <div class="order-summary">
            <div class="summary-item">
              <span class="label">门票费用</span>
              <span class="value">¥{{ ticketTotal }}</span>
            </div>
            <div class="summary-divider"></div>
            <div class="summary-total">
              <span class="label">应付金额</span>
              <span class="amount">¥{{ ticketTotal }}</span>
            </div>
          </div>

          <!-- 提交按钮 -->
          <div class="submit-section">
            <el-button class="cancel-btn" size="large" @click="handleClose">取消订单</el-button>
            <el-button class="pay-btn" type="primary" size="large" :loading="paying" @click="handlePay">
              立即支付
            </el-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, onUnmounted } from 'vue'
import { useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Location, User, Phone, CircleCheck, Clock } from '@element-plus/icons-vue'
// import { createOrder } from '@/api/order/index.js'

const route = useRoute()

// 景点信息
const attraction = ref({
  id: '',
  name: '',
  location: '',
  price: 0,
  image: ''
})

// 门票数量
const ticketCount = ref(1)

// 游玩日期
const visitDate = ref('')

// 联系人信息
const contactForm = ref({
  name: '',
  phone: ''
})

// 支付状态
const paying = ref(false)
const paymentSuccess = ref(false)
const countdown = ref(3)
let countdownTimer = null

// 计算总价
const ticketTotal = computed(() => {
  return (attraction.value.price * ticketCount.value).toFixed(2)
})

// 禁用过去的日期
const disabledDate = (time) => {
  return time.getTime() < Date.now() - 8.64e7
}

// 关闭窗口
const handleClose = () => {
  window.close()
}

// 处理支付
const handlePay = async () => {
  // 表单验证
  if (!visitDate.value) {
    ElMessage.warning('请选择游玩日期')
    return
  }
  if (!contactForm.value.name) {
    ElMessage.warning('请输入联系人姓名')
    return
  }
  if (!contactForm.value.phone) {
    ElMessage.warning('请输入联系人手机号')
    return
  }
  if (!/^1[3-9]\d{9}$/.test(contactForm.value.phone)) {
    ElMessage.warning('请输入正确的手机号')
    return
  }

  paying.value = true

  try {
    // TODO: 调用支付接口
    // await createOrder({
    //   attractionId: attraction.value.id,
    //   ticketCount: ticketCount.value,
    //   visitDate: visitDate.value,
    //   contact: contactForm.value,
    //   totalAmount: ticketTotal.value
    // })

    // 模拟支付延迟
    await new Promise(resolve => setTimeout(resolve, 1000))

    // 显示支付成功页面
    paymentSuccess.value = true
    
    // 启动倒计时
    startCountdown()
  } catch (error) {
    ElMessage.error('支付失败，请重试')
  } finally {
    paying.value = false
  }
}

// 启动倒计时
const startCountdown = () => {
  countdown.value = 3
  countdownTimer = setInterval(() => {
    countdown.value--
    if (countdown.value <= 0) {
      clearInterval(countdownTimer)
      window.close()
    }
  }, 1000)
}

// 初始化数据
onMounted(() => {
  // 从URL参数获取景点信息
  const { id, name, location, price, image } = route.query
  
  if (id) {
    attraction.value = {
      id: id || '',
      name: name ? decodeURIComponent(name) : '',
      location: location ? decodeURIComponent(location) : '',
      price: Number(price) || 0,
      image: image ? decodeURIComponent(image) : ''
    }
  }

  // 设置默认游玩日期为明天
  const tomorrow = new Date()
  tomorrow.setDate(tomorrow.getDate() + 1)
  visitDate.value = tomorrow.toISOString().split('T')[0]
})

// 清理定时器
onUnmounted(() => {
  if (countdownTimer) {
    clearInterval(countdownTimer)
  }
})
</script>

<style scoped lang="less">
.order-page {
  min-height: 100vh;
  background: #f5f7fa;
}

.order-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 40px 20px 60px;
}

// 支付成功页面
.success-page {
  min-height: calc(100vh - 100px);
  display: flex;
  align-items: center;
  justify-content: center;
}

.success-content {
  text-align: center;
  max-width: 500px;
  width: 100%;
}

.success-icon {
  margin-bottom: 24px;
  animation: scaleIn 0.5s ease-out;

  .check-icon {
    font-size: 120px;
    color: #67c23a;
    filter: drop-shadow(0 4px 12px rgba(103, 194, 58, 0.3));
  }
}

.success-title {
  font-size: 36px;
  font-weight: 700;
  color: #1a1a1a;
  margin: 0 0 12px 0;
  animation: fadeInUp 0.6s ease-out 0.2s both;
}

.success-message {
  font-size: 16px;
  color: #666;
  margin: 0 0 32px 0;
  animation: fadeInUp 0.6s ease-out 0.3s both;
}

.order-info-card {
  background: #fff;
  border-radius: 16px;
  padding: 24px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  margin-bottom: 24px;
  animation: fadeInUp 0.6s ease-out 0.4s both;

  .info-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 0;
    border-bottom: 1px solid #f0f0f0;

    &:last-child {
      border-bottom: none;
    }

    &.highlight {
      padding-top: 16px;
      margin-top: 8px;
      border-top: 2px solid #f0f0f0;

      .label {
        font-size: 18px;
        font-weight: 600;
        color: #1a1a1a;
      }

      .value {
        font-size: 28px;
        font-weight: 700;
        color: #ff6b6b;
      }
    }

    .label {
      font-size: 15px;
      color: #999;
    }

    .value {
      font-size: 16px;
      font-weight: 500;
      color: #1a1a1a;
    }
  }
}

.countdown-tip {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-size: 14px;
  color: #999;
  margin-bottom: 24px;
  animation: fadeInUp 0.6s ease-out 0.5s both;

  .el-icon {
    font-size: 16px;
  }
}

.success-content .close-btn {
  width: 100%;
  height: 52px;
  font-size: 16px;
  font-weight: 600;
  border-radius: 12px;
  background: linear-gradient(135deg, #ff6b6b 0%, #ff8e53 100%);
  border: none;
  color: #fff;
  animation: fadeInUp 0.6s ease-out 0.6s both;

  &:hover {
    opacity: 0.9;
  }
}

@keyframes scaleIn {
  from {
    transform: scale(0);
    opacity: 0;
  }
  to {
    transform: scale(1);
    opacity: 1;
  }
}

@keyframes fadeInUp {
  from {
    transform: translateY(20px);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}

// 内容区域
.order-content {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

// 页面头部
.page-header {
  text-align: center;
  margin-bottom: 16px;

  .page-title {
    font-size: 32px;
    font-weight: 700;
    color: #1a1a1a;
    margin: 0 0 8px 0;
  }

  .page-subtitle {
    font-size: 15px;
    color: #999;
    margin: 0;
  }
}

// 景点信息
.attraction-section {
  margin-bottom: 8px;
}

.attraction-banner {
  position: relative;
  width: 100%;
  height: 280px;
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);

  .banner-image {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }

  .banner-overlay {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 32px 24px 24px;
    background: linear-gradient(to top, rgba(0, 0, 0, 0.7) 0%, transparent 100%);
    color: #fff;

    .attraction-title {
      font-size: 28px;
      font-weight: 700;
      margin: 0 0 12px 0;
      text-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
    }

    .attraction-location {
      display: flex;
      align-items: center;
      gap: 6px;
      font-size: 16px;
      opacity: 0.95;

      .el-icon {
        font-size: 18px;
      }
    }
  }
}

// 订单表单
.order-form {
  background: #fff;
  border-radius: 16px;
  padding: 32px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
}

.form-section {
  margin-bottom: 32px;

  &:last-of-type {
    margin-bottom: 0;
  }
}

.form-title {
  font-size: 18px;
  font-weight: 600;
  color: #1a1a1a;
  margin: 0 0 16px 0;
}

// 门票选择
.ticket-selector {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  background: linear-gradient(135deg, #fff5f5 0%, #fff8f0 100%);
  border-radius: 12px;
  border: 2px solid rgba(255, 107, 107, 0.1);
}

.ticket-info {
  .ticket-name {
    font-size: 18px;
    font-weight: 600;
    color: #1a1a1a;
    margin-bottom: 4px;
  }

  .ticket-price {
    font-size: 20px;
    font-weight: 700;
    color: #ff6b6b;
  }
}

.ticket-counter {
  display: flex;
  align-items: center;
  gap: 12px;

  .counter-label {
    font-size: 15px;
    color: #666;
  }
}

// 联系人表单
.contact-form {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

// 订单汇总
.order-summary {
  margin-top: 32px;
  padding: 24px;
  background: linear-gradient(135deg, #f8f9fa 0%, #f5f7fa 100%);
  border-radius: 12px;
}

.summary-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 16px;
  color: #666;

  .value {
    font-weight: 500;
    color: #1a1a1a;
  }
}

.summary-divider {
  height: 1px;
  background: linear-gradient(to right, transparent, #ddd, transparent);
  margin: 16px 0;
}

.summary-total {
  display: flex;
  justify-content: space-between;
  align-items: center;

  .label {
    font-size: 18px;
    font-weight: 600;
    color: #1a1a1a;
  }

  .amount {
    font-size: 32px;
    font-weight: 700;
    color: #ff6b6b;
  }
}

// 提交按钮
.submit-section {
  margin-top: 32px;
  display: flex;
  gap: 16px;

  .el-button {
    flex: 1;
    height: 52px;
    font-size: 16px;
    font-weight: 600;
    border-radius: 12px;
  }

  .cancel-btn {
    background: #f5f7fa;
    border: none;
    color: #666;

    &:hover {
      background: #e8eaed;
    }
  }

  .pay-btn {
    background: linear-gradient(135deg, #ff6b6b 0%, #ff8e53 100%);
    border: none;
    box-shadow: 0 4px 12px rgba(255, 107, 107, 0.3);

    &:hover {
      box-shadow: 0 6px 20px rgba(255, 107, 107, 0.4);
      transform: translateY(-2px);
    }

    &:active {
      transform: translateY(0);
    }
  }
}

@media (max-width: 768px) {
  .order-container {
    padding: 24px 16px 40px;
  }

  .success-icon {
    .check-icon {
      font-size: 100px;
    }
  }

  .success-title {
    font-size: 28px;
  }

  .success-message {
    font-size: 15px;
  }

  .order-info-card {
    padding: 20px;

    .info-item.highlight {
      .value {
        font-size: 24px;
      }
    }
  }

  .page-header {
    .page-title {
      font-size: 26px;
    }
  }

  .attraction-banner {
    height: 220px;

    .banner-overlay {
      padding: 24px 20px 20px;

      .attraction-title {
        font-size: 22px;
      }

      .attraction-location {
        font-size: 14px;
      }
    }
  }

  .order-form {
    padding: 24px 20px;
  }

  .form-title {
    font-size: 16px;
  }

  .ticket-selector {
    flex-direction: column;
    align-items: flex-start;
    gap: 16px;
    padding: 16px;
  }

  .ticket-counter {
    width: 100%;
    justify-content: space-between;
  }

  .order-summary {
    padding: 20px;
  }

  .summary-total {
    .amount {
      font-size: 28px;
    }
  }

  .submit-section {
    flex-direction: column;

    .el-button {
      width: 100%;
    }
  }
}
</style>
